using System;
using System.Linq;
using System.Web.Configuration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

using Aks.Deployment.SharePoint.Configuration;
using Aks.Web.UI.WebControls.WebParts.Adapters;

namespace Aks.Deployment.Features
{
	/// <summary>
	/// Infrastructure feature receiver.
	/// </summary>
	public class InfrastructureFeatureReceiver : SPFeatureReceiver
	{
		#region | Fields |

		private const string FeatureScopeInvalidErrorFormat = "This feature only supports being activated at the web application level ({0})";
		private const string FeatureOwner = "AksInfrastructure";

		#endregion


		#region | Event Handlers |

		/// <summary>
		/// Occurs after a Feature is activated.
		/// </summary>
		/// <param name="properties">An <see cref="SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
		public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
			// get web app
			var app = properties.Feature.Parent as SPWebApplication;
			if (app == null)
			{
				throw new InvalidOperationException(String.Format(FeatureScopeInvalidErrorFormat, properties.Feature.Parent.GetType()));
			}

			// add web.config modifications
			AddWebConfigModifications(app);
			InfrastructureSetupJob.Activate(app);
		}


		/// <summary>
		/// Occurs when a Feature is deactivated.
		/// </summary>
		/// <param name="properties">An <see cref="SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
		public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
		{
			// get web app
			var app = properties.Feature.Parent as SPWebApplication;
			if (app == null)
			{
				throw new InvalidOperationException(String.Format(FeatureScopeInvalidErrorFormat, properties.Feature.Parent.GetType()));
			}

			// add web.config modifications
			RemoveWebConfigModifications(app);
			InfrastructureSetupJob.Deactivate(app);
		}


		/// <summary>
		/// Occurs after a Feature is installed.
		/// </summary>
		/// <param name="properties">An <see cref="SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
		public override void FeatureInstalled(SPFeatureReceiverProperties properties)
		{}


		/// <summary>
		/// Occurs when a Feature is uninstalled.
		/// </summary>
		/// <param name="properties">An <see cref="SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
		public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
		{}

		#endregion


		#region | Methods [Private] |

		/// <summary>
		/// Adds the web.config modifications.
		/// </summary>
		/// <param name="app">The web application to make changes to.</param>
		private static void AddWebConfigModifications(SPWebApplication app)
		{
			// init vars
			var config = new ConfigurationManager(app, FeatureOwner);

			// check to see if web.config modifications have already been made
			if (config.ContainsAny())
			{
				return;
			}

			// add web config mods
			config.AddCompilationAssembly(typeof(WebPartZoneAdapter))
				.AddCompilationAssembly(typeof(CSSFriendly.MenuAdapter))
				.SetXhtmlConformance(XhtmlConformanceMode.Strict)
				.Update();
		}


		/// <summary>
		/// Removes the web.config modifications.
		/// </summary>
		/// <param name="app">The app.</param>
		private static void RemoveWebConfigModifications(SPWebApplication app)
		{
			new ConfigurationManager(app, FeatureOwner).RemoveAll().Update();
		}

		#endregion
	}
}